
import {
  program
} from "commander";
import {
  loadThenParseToHtml,
  commonHandleParseToHtmlResult,
  generateEPub
} from "@src/util-helper/epub-helper";
import { nodePath } from "@src/util-helper/path-helper";

async function main() {
  console.log(process.argv);
  program
    .option("--file-name <originFileName>", "需要解析的原电子书文件。请输入相对于static目录的相对路径。\n--parse时，此项必须要输入")
    .option("--book-name <bookCacheName>", "用作缓存的目录名和生成的书名,不带后缀名")
    .option("--parse")
    .option("--generate");
  program.parse();
  const options = program.opts();
  const originFileName = options.fileName || "";
  const bookCacheName = options.bookName || nodePath.parse(originFileName).name || "book";
  const isParse = options.parse ?? false;
  const isGenerate = options.generate ?? false;
  if (isParse) {
    if (!originFileName) {
      console.log("缺少必要的参数--file-name");
      return;
    }
    const {resultList, bookCachePath, epubInfo} = await loadThenParseToHtml(originFileName, bookCacheName);
    await commonHandleParseToHtmlResult(epubInfo, resultList, nodePath.join(bookCachePath, "fail-log.txt"));
  }
  if (isGenerate) {
    await generateEPub(bookCacheName, {
      verbose: true
    });
  }
}

main().catch((err) => {
  console.error(err);
});
